﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tasks.Common.Entitys;
namespace Tasks.Dao
{

    /// <summary><c>PWRole2MenuDao</c> is the implementation of <see cref="IPWRole2MenuDao"/>.</summary>
    public partial class PWRole2MenuDao : Mapper
    {

        /// <summary>Implements <see cref="IPWRole2MenuDao.GetCount"/></summary>
        public int GetCount()
        {
            String stmtId = "PWRole2Menu.GetCount";
            int result = ExecuteQueryForObject<int>(stmtId, null);
            return result;
        }

        /// <summary>Implements <see cref="IPWRole2MenuDao.Find"/></summary>
        public PWRole2Menu Find(Int32 iD)
        {
            String stmtId = "PWRole2Menu.Find";
            PWRole2Menu result = ExecuteQueryForObject<PWRole2Menu>(stmtId, iD);
            return result;
        }

        /// <summary>Implements <see cref="IPWRole2MenuDao.FindAll"/></summary>
        public IList<PWRole2Menu> FindAll()
        {
            String stmtId = "PWRole2Menu.FindAll";
            IList<PWRole2Menu> result = ExecuteQueryForList<PWRole2Menu>(stmtId, null);
            return result;
        }

        /// <summary>Implements <see cref="IPWRole2MenuDao.FindByRoleID"/></summary>
        public IList<PWRole2Menu> FindByRoleID(Int32? roleID)
        {
            String stmtId = "PWRole2Menu.FindByRoleID";
            IList<PWRole2Menu> result = ExecuteQueryForList<PWRole2Menu>(stmtId, roleID);
            return result;
        }

        public IList<PWRole2Menu> FindByUserID(int userID)
        {
            String stmtId = "PWRole2Menu.FindByUserID";
            IList<PWRole2Menu> result = ExecuteQueryForList<PWRole2Menu>(stmtId, userID);
            return result;
        }

        public IList<PWRole2Menu> FindParentIDByUserID(int userID)
        {
            String stmtId = "PWRole2Menu.FindParentIDByUserID";
            IList<PWRole2Menu> result = ExecuteQueryForList<PWRole2Menu>(stmtId, userID);
            return result;
        }

     
        /// <summary>Implements <see cref="IPWRole2MenuDao.FindByMenuID"/></summary>
        public IList<PWRole2Menu> FindByMenuID(Int32? menuID)
        {
            String stmtId = "PWRole2Menu.FindByMenuID";
            IList<PWRole2Menu> result = ExecuteQueryForList<PWRole2Menu>(stmtId, menuID);
            return result;
        }


        /// <summary>Implements <see cref="IPWRole2MenuDao.Insert"/></summary>
        public void Insert(IList<PWRole2Menu> obj, int roleID)
        {
            SqlMapper.BeginTransaction();
            try
            {
                //先把原来的删除再添加
                this.DeleteByRoleID(roleID);
                foreach (PWRole2Menu item in obj)
                {
                    SqlMapper.Insert("PWRole2Menu.Insert", item);
                }
                SqlMapper.CommitTransaction();
            }
            catch (Exception ex)
            {
                SqlMapper.RollBackTransaction();
                throw ex;
            }

        }

        /// <summary>Implements <see cref="IPWRole2MenuDao.Update"/></summary>
        public void Update(PWRole2Menu obj)
        {
            if (obj == null) throw new ArgumentNullException("obj");
            String stmtId = "PWRole2Menu.Update";
            ExecuteUpdate(stmtId, obj);
        }

        /// <summary>Implements <see cref="IPWRole2MenuDao.Delete"/></summary>
        public void Delete(PWRole2Menu obj)
        {
            if (obj == null) throw new ArgumentNullException("obj");
            String stmtId = "PWRole2Menu.Delete";
            ExecuteDelete(stmtId, obj);
        }
        public void DeleteByRoleID(int roleID)
        {
            String stmtId = "PWRole2Menu.DeleteByRoleID";
            ExecuteDelete(stmtId, roleID);
        }
    }

}
